package com.itheima.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import java.text.SimpleDateFormat;
import java.util.*;

/*
 * 统计报表
 * */
@RequestMapping("/report")
public class ReportController {

    @Autowired
    private MemberService memberService;

    @RequestMapping("/getMemberReport")
    public Result getMemberReport() {
        //需要数据月份,和每个月的会员的数量
        //因为是键值对,所以需要用到map集合
        try {
            Map map = new HashMap();
            List<String> list = new ArrayList<>();
            //获得当月之前的月份
            Calendar instance = Calendar.getInstance();//获得日历对象
            instance.add(Calendar.MONTH, -12);
            //前12个月的月份
            for (int i = 0; i < 12; i++) {
                instance.add(Calendar.MONTH, 1);
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM");
                list.add(simpleDateFormat.format(instance.getTime()));
            }
            map.put("months", list);
            List<Integer> memberCount = memberService.findMemberCountByMonth(list);
            map.put("memberCount", memberCount);
            return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }

    @RequestMapping("/getSetmealReport")
    public Result getSetmealReport() {
        //需要数据月份,和每个月的会员的数量
        //因为是键值对,所以需要用到map集合
        try {
            Map map = new HashMap();
            List<String> list = new ArrayList<>();
            //获得当月之前的月份
            Calendar instance = Calendar.getInstance();
            instance.add(Calendar.MONTH, -12);
            //前12个月的月份
            for (int i = 0; i < 12; i++) {
                instance.add(Calendar.MONTH, 1);
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM");
                list.add(simpleDateFormat.format(instance.getTime()));
            }
            map.put("months", list);
            List<Integer> memberCount = memberService.findMemberCountByMonth(list);
            map.put("memberCount", memberCount);
            return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }
}
